package com.samoilovich.belarustoday.adapters;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.datemodel.RatesModel;

import android.app.Activity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class RatesAdapter extends BaseAdapter {
	private Activity mContext;
	private ArrayList<RatesModel> mItems;
	private int mLayoutRes;

    private boolean mWasFirstClick = false;
    private int mTimeout = ViewConfiguration.getDoubleTapTimeout();

    private Comparator<RatesModel> mBankAscendingComparator = new Comparator<RatesModel>() {
        @Override
        public int compare(RatesModel ratesModel, RatesModel ratesModel2) {
            return ratesModel.getBank().compareToIgnoreCase(ratesModel2.getBank());
        }
    };
    private Comparator<RatesModel> mBankDescendingComparator = new Comparator<RatesModel>() {
        @Override
        public int compare(RatesModel ratesModel, RatesModel ratesModel2) {
            return -ratesModel.getBank().compareToIgnoreCase(ratesModel2.getBank());
        }
    };
    // -----------------------------------------------------------------------------------
    private Comparator<RatesModel> mBuyingAscendingComparator = new Comparator<RatesModel>() {
        @Override
        public int compare(RatesModel ratesModel, RatesModel ratesModel2) {
            return ratesModel.getBuying().compareToIgnoreCase(ratesModel2.getBuying());
        }
    };
    private Comparator<RatesModel> mBuyingDescendingComparator = new Comparator<RatesModel>() {
        @Override
        public int compare(RatesModel ratesModel, RatesModel ratesModel2) {
            return -ratesModel.getBuying().compareToIgnoreCase(ratesModel2.getBuying());
        }
    };
    // -----------------------------------------------------------------------------------
    private Comparator<RatesModel> mSaleAscendingComparator = new Comparator<RatesModel>() {
        @Override
        public int compare(RatesModel ratesModel, RatesModel ratesModel2) {
            return ratesModel.getSale().compareToIgnoreCase(ratesModel2.getSale());
        }
    };
    private Comparator<RatesModel> mSaleDescendingComparator = new Comparator<RatesModel>() {
        @Override
        public int compare(RatesModel ratesModel, RatesModel ratesModel2) {
            return -ratesModel.getSale().compareToIgnoreCase(ratesModel2.getSale());
        }
    };
    private GestureDetector mBankGestureDetector =  new GestureDetector(mContext, new BankNameGestureListener());
    private GestureDetector mBuyingGestureDetector = new GestureDetector(mContext, new  BuyingGestureListener());
    private GestureDetector mSaleGestureDetector =  new GestureDetector(mContext, new SaleGestureListener());

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
	public RatesAdapter(Activity context, ArrayList<RatesModel> items, int layoutRes) {
		mContext = context;
		mItems = items;
		mLayoutRes = layoutRes;
        if (mItems != null) {
            Collections.sort(mItems, mBankAscendingComparator);
        }
	}

	public int getCount() {
		if (mItems != null) {
			return mItems.size();
		}
		
		return 0;
	}

	public Object getItem(int position) {
		if (mItems != null && mItems.size() > position) {
			return mItems.get(position);
		}
		
		return null;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		RatesModel currentItem;
		
		currentItem = mItems.get(position);
		if (convertView == null) {
			convertView = mContext.getLayoutInflater().inflate(mLayoutRes, null);
			holder = new ViewHolder();
			holder.BankName = (TextView) convertView.findViewById(R.id.textView_bank);
			holder.Buying = (TextView) convertView.findViewById(R.id.textView_buying);
			holder.Sale = (TextView) convertView.findViewById(R.id.textView_sale);
            holder.BankName.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    return mBankGestureDetector.onTouchEvent(event);
                }
            });
            holder.Buying.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    return mBuyingGestureDetector.onTouchEvent(event);
                }
            });
            holder.Sale.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    return mSaleGestureDetector.onTouchEvent(event);
                }
            });
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.BankName.setText(currentItem.getBank());
		holder.Buying.setText(currentItem.getBuying());
		holder.Sale.setText(currentItem.getSale());
		convertView.setTag(holder);
		
		return convertView;
	}

    public void setRates(ArrayList<RatesModel> info) {
        mItems = info;
        if (mItems != null) {
            Collections.sort(mItems, mBankAscendingComparator);
        }
        notifyDataSetChanged();
    }
	
	// -----------------------------------------------------------------------------------
	private class ViewHolder {
		TextView BankName;
		TextView Buying;
		TextView Sale;
	}

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    private class BankNameGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            Collections.sort(mItems, mBankAscendingComparator);
            notifyDataSetChanged();

            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent event) {
            Collections.sort(mItems, mBankDescendingComparator);
            notifyDataSetChanged();

            return true;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent event) {
            return true;
        }

    }

    private class BuyingGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            Collections.sort(mItems, mBuyingAscendingComparator);
            notifyDataSetChanged();

            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent event) {
            Collections.sort(mItems, mBuyingDescendingComparator);
            notifyDataSetChanged();

            return true;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent event) {
            return true;
        }

    }

    private class SaleGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            Collections.sort(mItems, mSaleAscendingComparator);
            notifyDataSetChanged();

            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent event) {
            Collections.sort(mItems, mSaleDescendingComparator);
            notifyDataSetChanged();

            return true;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent event) {
            return true;
        }

    }
}
